package tako.wisers.com.android.apis.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import tako.wisers.com.android.apis.R;
import tako.wisers.com.android.apis.database.SQListManager;
import tako.wisers.com.android.apis.parser.ConfXMLParser;
import tako.wisers.com.android.apis.parser.xml.item.Draw;
import tako.wisers.com.android.apis.view.ViewNameIDConverse;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;

public class BackGroundHelper {
	public synchronized static void settingBackGround(Activity activity, Integer layoutID){
		ConfXMLParser confXMLParser = ConfXMLParser.getInstance();
		Map<String, Draw> backGroundMap = confXMLParser.getDrawListByLayout(ViewNameIDConverse.getNameFromId(layoutID));
		if(backGroundMap == null) return ;
		Iterator<Draw> itr = backGroundMap.values().iterator();
		while(itr.hasNext()){
			Draw draw = itr.next();
			View view = activity.findViewById(ViewNameIDConverse.getIdFromName(draw.getViewID()));
			if(view == null) continue;
			Drawable background = new BitmapDrawable(draw.getBackGround());
			view.setBackgroundDrawable(background);
		}
	}
	
	private static String filte = "viewName='%s' AND layoutName='%s'";
	
	public static void settingInitBackGround(Activity activity){
		SQListManager sqlManager = SQListManager.getInstance();
		String layoutName = ViewNameIDConverse.getNameFromId(R.layout.loading_page);
		String viewName = ViewNameIDConverse.getNameFromId(R.id.loading_indicator);
		String selection = String.format(filte, viewName, layoutName);
		List<Draw> drawList = sqlManager.select(Draw.class, false, selection);
		if(drawList != null && drawList.size() > 0){
			Draw draw = drawList.get(0);
			if(draw.getBackGround() != null){
				View view = activity.findViewById(R.id.loading_indicator);
				Drawable background = new BitmapDrawable(draw.getBackGround());
				view.setBackgroundDrawable(background);
			}
		}
	}
}
